//
//  RightViewController.m
//  GSG
//
//  Created by gaox on 15/4/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RightViewController.h"
#import "AboutViewController.h"
@interface RightViewController ()<UIAlertViewDelegate>
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UIAlertView *alertView;
@end

@implementation RightViewController

- (void)dealloc
{
    [_label release];
    [_alertView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cf.jpg"]];
    
    

//    UIButton *bu1=[UIButton buttonWithType:UIButtonTypeSystem];
//    bu1.frame = CGRectMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 140 / 667, self.view.frame.size.width * 218 / 375, self.view.frame.size.height * 38 / 667);
////    [bu1 setTitle:@"更换背景" forState:UIControlStateNormal];
//    bu1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"收藏.png"]];
//    bu1.titleLabel.font = [UIFont systemFontOfSize:20];
//    [bu1 addTarget:self action:@selector(actionBu1) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:bu1];

    
//    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 120 / 375, self.view.frame.size.height * 135 /667, self.view.frame.size.width * 30 / 375, self.view.frame.size.height * 30 / 667)];
//    [aView setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:aView];
    
    
    UIButton *bu2=[UIButton buttonWithType:UIButtonTypeCustom];
    bu2.frame = CGRectMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 240 / 667, self.view.frame.size.width * 218 / 375, self.view.frame.size.height * 38 / 667);

    [bu2 setImage:[UIImage imageNamed:@"清除缓存.png"] forState:UIControlStateNormal];
    bu2.titleLabel.font = [UIFont systemFontOfSize:20];
    [bu2 addTarget:self action:@selector(actionBu2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bu2];
    
//    UIView *aView1 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 120 / 375, self.view.frame.size.height * 205 /667, self.view.frame.size.width * 30 / 375, self.view.frame.size.height * 30 / 667)];
//    [aView1 setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:aView1];

    
    
    UIButton *bu3=[UIButton buttonWithType:UIButtonTypeCustom];
    bu3.frame = CGRectMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 350 / 667, self.view.frame.size.width * 218 / 375, self.view.frame.size.height * 38 / 667);
//    [bu3 setTitle:@"关于我们" forState:UIControlStateNormal];
//    bu3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"关于我们.png"]];
    [bu3 setImage:[UIImage imageNamed:@"关于我们.png"] forState:UIControlStateNormal];
    bu3.titleLabel.font = [UIFont systemFontOfSize:20];
    [bu3 addTarget:self action:@selector(actionBu3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bu3];
    


    

}

//- (void)actionBu1{

//    self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.height * 100 / 256, self.view.frame.size.height * 100 / 351, self.view.frame.size.height * 100 / 667, self.view.frame.size.height * 100 / 1111)];
//    NSString *str = [NSString stringWithFormat:@"共%.2fM",self.folderSize];
//    self.label.text = str;
//    self.label.textColor = [UIColor blueColor];
//    self.label.font = [UIFont systemFontOfSize:25];
//    [self.view addSubview:self.label];
//    [_label release];
//}





- (void)actionBu2{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    //    [self folderSizeAtPath:sandBoxPath];
    NSString *str = [NSString stringWithFormat:@"共%.2fM缓存文件",[self folderSizeAtPath:sandBoxPath]];
    
    
    self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: @"取消",nil];
    [self.alertView show];
    [_alertView release];
    
    //    [self clearCache:sandBoxPath];
//    NSLog(@"%@",sandBoxPath);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView == self.alertView) {
        if (buttonIndex == 0) {
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *sandBoxPath = [sandBox lastObject];
            [self clearCache:sandBoxPath];
        }
        
    }
    
}
- (void)actionBu3{


    AboutViewController *video = [[AboutViewController alloc] init];
    [video setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:video animated:YES completion:^{
        
        
    }];

}
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
}
// 计算单个文件大小
- (CGFloat)fileSizeAtPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //   self.folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        CGFloat folderSize = 0;
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        return folderSize;
    }
    //    NSLog(@"%.2fM",self.folderSize);
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
